package com.zlll.winner.service.common;

import com.zlll.winner.business.service.common.IEnumService;
import com.zlll.winner.util.ConstantUtils;
import org.apache.dubbo.config.annotation.Service;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EnumServiceImpl implements IEnumService {

    @Override
    public Map<String, String> getBaseEnumMap(String enumType) {
        return ConstantUtils.getEnumList("com.zlll.winner.enums",enumType);
    }

    @Override
    public Map<String, String> getBaseEnumMap(String packagePath, String enumType) {
        return ConstantUtils.getEnumList(packagePath,enumType);
    }

    @Override
    public Object getEnumByPath(String packagePath) throws Exception {
        // 1.得到枚举类对象
        Class<?> clz = Class.forName(packagePath);
        // 2.得到所有枚举常量
        Object[] objects = clz.getEnumConstants();
        Method getCode = clz.getMethod("getValue");
        Method getName = clz.getMethod("getDesc");
        List<Map<String, Object>> data = new ArrayList<>(objects.length);
        for (Object obj : objects){
            Map<String, Object> map = new HashMap<>(2);
            // 3.调用对应方法，得到枚举常量中字段的值
            map.put("id", String.valueOf(getCode.invoke(obj)));
            map.put("content", String.valueOf(getName.invoke(obj)));
            data.add(map);
        }
        return data;
    }
}
